package com.opo5.logisticsmanagement.controller;

import com.opo5.logisticsmanagement.Vo.RoleMenuParamVo;
import com.opo5.logisticsmanagement.Vo.UserVo;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.domain.Department;
import com.opo5.logisticsmanagement.domain.Role;
import com.opo5.logisticsmanagement.service.*;
import com.opo5.logisticsmanagement.validationgroup.InsertValida;
import com.opo5.logisticsmanagement.validationgroup.UpdateValida;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Title: UserController
 * @Author: lixin
 * @Date: 2022/07/10/09:20
 * @Description: 员工管理
 */
@RestController
@RequestMapping("/user")
@Validated
@CrossOrigin
public class UserManageController {

    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * @Author: lixin
     * @Description: 按条件查询所有员工信息
     * @param nickname 员工用户名
     * @param currentPage 当前页
     * @param pageSize 几条数据
     */
    @GetMapping(value={
            "/getUserByCondition/{nickname}/{currentPage}/{pageSize}",
            "/getUserByCondition/{currentPage}/{pageSize}"
    })
    public R getUserByCondition(@PathVariable(value = "nickname",required = false) String nickname,@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        R r = userService.getUserByCondition(nickname,currentPage,pageSize);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 根据id查询员工信息
     * @param id 员工id
     */
    @GetMapping("/getUserById/{id}")
    public R getUserById(@PathVariable(value = "id") Integer id){
        R r = userService.getUserById(id);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 添加用户
     * @param userVo 封装了用户信息、用户地址
     */
    @PostMapping("/addUser")
    public R addUser(@Validated(InsertValida.class) @RequestBody UserVo userVo){
        R r = userService.addUser(userVo);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 修改用户
     * @param userVo 封装了用户信息、用户地址
     */
    @PutMapping("/editUser")
    public R editUser(@Validated(UpdateValida.class) @RequestBody UserVo userVo){
        R r = userService.editUser(userVo);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 删除用户
     * @param ids 多个id
     */
    @DeleteMapping("/delUser")
    public R delUser(@RequestBody @NotEmpty(message = "ids不能为空") Integer[] ids){
        System.out.println(ids);
        R r = userService.delUser(ids);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 按条件查询所有部门信息
     * @param departmentname 部门名
     * @param currentPage 当前页
     * @param pageSize 几条数据
     */
    @GetMapping(value={
            "/getDeptByCondition/{departmentname}/{currentPage}/{pageSize}",
            "/getDeptByCondition/{currentPage}/{pageSize}"
    })
    public R getDeptByCondition(@PathVariable(value = "departmentname",required = false) String departmentname,@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        R r = departmentService.getDeptByCondition(departmentname,currentPage,pageSize);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 按条件查询所有部门信息
     */
    @GetMapping("/getDeptAndRoleAll")
    public R getDeptAndRoleAll(){
        List<Department> departments =  departmentService.getDeptAll();
        List<Role> roles = roleService.getRoleAll();
        return R.success(new R().add("departments",departments).add("roles",roles).getMap());
    }

    /**
     * @Author: lixin
     * @Description: 根据id查询部门信息
     * @param id 部门id
     */
    @GetMapping("/getDeptById/{id}")
    public R getDeptById(@PathVariable(value = "id") Integer id){
        R r = departmentService.getUserById(id);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 添加部门
     * @param department 部门信息
     */
    @PostMapping("/addDept")
    public R addDept(@Validated(InsertValida.class) @RequestBody Department department){
        R r = departmentService.addDept(department);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 修改部门
     * @param Department 部门信息
     */
    @PutMapping("/editDept")
    public R editDept(@Validated(UpdateValida.class) @RequestBody Department Department){
        R r = departmentService.editDept(Department);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 删除部门
     * @param ids 多个id
     */
    @DeleteMapping("/delDept")
    public R delDept(@RequestBody @NotEmpty(message="id不能为空") Integer[] ids){
        R r = departmentService.delDept(ids);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 按条件查询所有角色信息
     * @param name 角色名
     * @param currentPage 当前页
     * @param pageSize 几条数据
     */
    @GetMapping(value={
            "/getRoleByCondition/{name}/{currentPage}/{pageSize}",
            "/getRoleByCondition/{currentPage}/{pageSize}"
    })
    public R getRoleByCondition(@PathVariable(value = "name",required = false) String name,@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        R r = roleService.getRoleByCondition(name,currentPage,pageSize);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 根据id查询角色信息
     * @param id 角色id
     */
    @GetMapping("/getRoleById/{id}")
    public R getRoleById(@PathVariable(value = "id") @NotNull(message="id不能为空") Integer id){
        R r = roleService.getRoleById(id);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 添加角色
     * @param role 角色信息
     */
    @PostMapping("/addRole")
    public R addRole(@Validated(InsertValida.class) @RequestBody Role role){
        R r = roleService.addRole(role);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 修改角色
     * @param role 角色信息
     */
    @PutMapping("/editRole")
    public R editRole(@Validated(UpdateValida.class) @RequestBody Role role){
        R r = roleService.editRole(role);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 删除角色
     * @param ids 多个id
     */
    @DeleteMapping("/delRole")
    public R delRole(@RequestBody @NotEmpty(message = "ids不能为空") Integer[] ids){
        R r = roleService.delRole(ids);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 获取所有菜单和角色拥有情况
     * @param id 角色id
     */
    @GetMapping("/getRoleMenuById/{id}")
    public R getRoleMenuById(@PathVariable("id") @NotNull(message = "id不能为空") Integer id){
        R r = menuService.getRoleMenuById(id);
        return r;
    }

    @PutMapping("/editRoleMenu")
    public R editRoleMenu(@RequestBody @Validated RoleMenuParamVo roleMenuParamVo){
        R r = roleMenuService.editRoleMenu(roleMenuParamVo);
        return r;
    }


}
